#!/usr/local/bin/perl -w
use strict;
use utf8;
use lib '../lib';

use NG::Gen::App qw(
NG::Gen::AuthCgi
);

use JSON::XS;
use NG::Sys::Cfg;
use Data::Dumper;

use NG::Gen::Users::Constants;
my $app = new NG::Sys::App();

$app->run;

sub on_before_process {
	my ($app,$form,$obj) = @_;
	unless($form->{user}){
		$form->set_error(400=>"need auth");
		return 0;
	}
	$form->{pause} = 0.1;
	return 1;
}
sub cmd_reference_load {
	my ($app,$form,$obj) = @_;
	if($obj){
		$form->{data} = $app->reference($obj)->get_list;
	}elsif(my $list = $form->{input}{list}){
		my %data;
		for(@$list){
			$data{$_} = $app->reference($_)->get_list;
		}
		$form->{data} = \%data;
	}else{
		$form->set_error(400=>"need reference name");
	}
	$form->process_as_json;
}
sub cmd_leaders_load {
	my ($app,$form,$obj) = @_;
	$form->{data} = $form->{user}->leaders->list;
	$form->process_as_json;
}
sub cmd_units_load {
	my ($app,$form,$obj) = @_;
	my $entry = $form->{input}{entry};
	unless($entry && ($entry = $form->{user}->leaders->get_by_entry($entry))){
		$form->set_error(501 => "bad entry");
		$form->process_as_json;
		return;
	}
	$form->{data} = $entry->units->list;
	$form->process_as_json;
}
sub cmd_items_load {
	my ($app,$form,$obj) = @_;
	my $entry = $form->{input}{entry};
	unless($entry && ($entry = $form->{user}->leaders->get_by_entry($entry))){
		$form->set_error(501 => "bad entry");
		$form->process_as_json;
		return;
	}
	$form->{data} = $entry->items->list;
	$form->process_as_json;
}
sub cmd_units_move {
	my ($app,$form,$obj) = @_;
	
	my $source = $form->{input}{source};
	my $target = $form->{input}{target};
	my $proto = $form->{input}{proto};
	my $count = $form->{input}{count};
	
	my $leaders = $form->{user}->leaders;
	unless($source && ($source = $leaders->get_by_entry($source))){
		$form->set_error(501 => "need source");
		$form->process_as_json;
		return;
	}
	unless($target && ($target = $leaders->get_by_entry($target))){
		$form->set_error(501 => "need target");
		$form->process_as_json;
		return;
	}
	unless($proto && ($proto = $app->reference("units")->get_by_key($proto))){
		$form->set_error(501 => "need proto");
		$form->process_as_json;
		return;
	}
	$app->logger->notice(sprintf 's=%d, t=%d, p=%d, c=%d',
					$source->entry,$target->entry,$proto->{entry},$count);
	if(my $error = $form->{user}->transfer_unit($source,$target,$proto,$count)){
		$form->set_error(501 => $error);
		$form->process_as_json;
		return;
	}
	$form->{data} = {
		source => $source->units->list,
		target => $target->units->list,
	};
	$form->process_as_json;
}
sub cmd_units_add {
	my ($app,$form,$obj) = @_;
	
	my $target = $form->{input}{leader};
	my $proto = $form->{input}{proto};
	
	my $leaders = $form->{user}->leaders;
	unless($target && ($target = $leaders->get_by_entry($target))){
		$form->set_error(501 => "need target");
		$form->process_as_json;
		return;
	}
	unless($target->proto->{type_i} == 1){
		$form->set_error(501 => "bad target,need typei == 1");
		$form->process_as_json;
		return;
	}
	unless($proto && ($proto = $app->reference("units")->get_by_key($proto))){
		$form->set_error(501 => "need proto");
		$form->process_as_json;
		return;
	}
	if(my $error = $form->{user}->add_unit($target,$proto)){
		$form->set_error(501 => $error);
		$form->process_as_json;
		return;
	}
	$form->{data} = $target->units->list,
	$form->process_as_json;
}
sub cmd_items_move {
	my ($app,$form,$obj) = @_;
	
	my $source = $form->{input}{source};
	my $target = $form->{input}{target};
	my $entry = $form->{input}{entry};
	
	my $leaders = $form->{user}->leaders;
	unless($source && ($source = $leaders->get_by_entry($source))){
		$form->set_error(501 => "need source");
		$form->process_as_json;
		return;
	}
	unless($target && ($target = $leaders->get_by_entry($target))){
		$form->set_error(501 => "need target");
		$form->process_as_json;
		return;
	}
	unless($entry){
		$form->set_error(501 => "need entry");
		$form->process_as_json;
		return;
	}
	if(my $error = $form->{user}->transfer_item($source,$target,$entry)){
		$form->set_error(501 => $error);
		$form->process_as_json;
		return;
	}
	$form->{data} = {
		source => $source->items->list,
		target => $target->items->list,
	};
	$form->process_as_json;
}
sub cmd_items_add {
	my ($app,$form,$obj) = @_;
	
	my $target = $form->{input}{leader};
	my $proto = $form->{input}{proto};
	
	my $leaders = $form->{user}->leaders;
	unless($target && ($target = $leaders->get_by_entry($target))){
		$form->set_error(501 => "need target");
		$form->process_as_json;
		return;
	}
	unless($target->proto->{type_i} == 1){
		$form->set_error(501 => "bad target,need typei == 1");
		$form->process_as_json;
		return;
	}
	unless($proto && ($proto = $app->reference("items")->get_by_key($proto))){
		$form->set_error(501 => "need proto");
		$form->process_as_json;
		return;
	}
	if(my $error = $form->{user}->add_item($target,$proto)){
		$form->set_error(501 => $error);
		$form->process_as_json;
		return;
	}
	$form->{data} = $target->items->list,
	$form->process_as_json;
}
sub cmd_task_current {
	my ($app,$form,$obj) = @_;
	my $leader = $form->{input}{entry};
	$form->{data} = $form->{user}->task->list;
	$form->process_as_json;
}
sub cmd_task_load {
	my ($app,$form,$obj) = @_;
	$form->{data} = $app->reference("tasks_common")->get_list;
	$form->process_as_json;
}